package com.jdk.study.d2jvm.chapter02;

/**
 * @description: 获取 ClassLoader 的途径
 * @author: JackLi
 * @date: 2020/4/11 16:11
 */
public class ClassLoader10_CustomClassLoader {

    public static void main(String[] args) {
        try {
            // 方式一：获取当前类的 ClassLoader  ==> clazz.getClassLoader()
            ClassLoader classLoader = Class.forName("java.lang.String").getClassLoader();
            System.out.println(classLoader);    // null
            System.out.println();

            // 方式二：获取当前线程上下文的 ClassLoader ==> Thread.currentThread().getContextClassLoader()
            ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
            System.out.println(contextClassLoader); // sun.misc.Launcher$AppClassLoader@18b4aac2
            System.out.println();

            // 方式三：获取系统的 ClassLoader ==> ClassLoader.getSystemClassLoader()
            ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
            System.out.println(systemClassLoader);  // sun.misc.Launcher$AppClassLoader@18b4aac2
            System.out.println();

            // 方式四：获取调用者的 ClassLoader ==> DriverManager.getCallerClassLoader()

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}
